<template>
  <div class='home-category' @mouseleave="currentId=null">
    <!-- 分类 -->
    <ul class="menu">
      <li  :class="{ 'active': currentId === item.id }" v-for="item in list" :key="item.id"
        @mouseenter="currentId = item.id">
        <RouterLink to="/">{{ item.name }}</RouterLink>
        <!-- 使用骨架屏的效果 -->
        <template v-if="item.children">
              <RouterLink  v-for="sub in item.children" :key="sub.id" to="/">{{ sub.name }}</RouterLink>
        </template>
        <span v-else>
          <XtxSkeleton animate width="60px" height="18px" style="margin-right:5px" bg="rgba(255,255,255,0.2)" />
          <XtxSkeleton animate width="50px" height="18px" bg="rgba(255,255,255,0.2)" />
        </span>
      </li>
    </ul>
    <!-- 弹层 -->
    <div class="layer">
      <h4>{{ currentId==='brand' ? '品牌' :'分类' }}推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <!-- 分类的推荐商品 -->
      <ul v-if="currentCate && currentCate.goods && currentCate.goods.length">
        <li v-for="item in currentCate.goods" :key="item.id">
          <RouterLink :to="`/product/${item.id}`">
            <img :src="item.picture" alt="">
            <div class="info">
              <p class="name ellipsis-2">{{ item.name }}</p>
              <p class="desc ellipsis">{{ item.desc }}</p>
              <p class="price"><i>¥</i>{{ item.price }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
      <!-- 推荐品牌 -->
      <ul v-if="currentCate && currentCate.brands && currentCate.brands.length">
        <li v-for="item in currentCate.brands" :key="item.id">
          <RouterLink to="/">
            <img :src="item.picture" alt="">
            <div class="info">
            <p class="place"><i class="iconfont icon-dingwei"></i>{{item.place}}</p>
            <p class="name ellipsis">{{item.name}}</p>
            <p class="desc ellipsis-2">{{item.desc}}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { findBrand } from '@/api/home'
export default {
  name: 'HomeCategory',
  setup() {
    // 伪造品牌数据
    const brand = reactive({
      id: 'brand',
      name: '品牌',
      children: [{ id: 'brand-chilren', name: '品牌推荐' }],
      brands: []
    })
    // 获取分类数据
    const store = useStore()
    const list = computed(() => {
      // console.log(store.state.cate.cateList)
      // 不能直接对vuex的数据做处理
      const cateList = store.state.cate.cateList.map(item => {
        return {
          id: item.id,
          name: item.name,
          goods: item.goods,
          children: item.children && item.children.slice(0, 2)
        }
      })
      cateList.push(brand)
      return cateList
    })

    // 获取品牌数据,如果使用async和await的写法，就必须要给setup()前加async,目前不支持这种写法
    findBrand(6).then(res => {
      // console.log(res)
      // 获取推荐品牌
      brand.brands = res.result
    })

    // 获取当前选中的分类
    // 定义当前的选中的分类id
    const currentId = ref(null)
    const currentCate = computed(() => {
      return list.value.find(item => item.id === currentId.value)
    })
    return { list, currentId, currentCate }
  }
}
</script>

<style scoped lang='less'>
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;

  // 弹层样式
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;

    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;

      small {
        font-size: 16px;
        color: #666;
      }
    }

    ul {
      display: flex;
      flex-wrap: wrap;

      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;

        &:nth-child(3n) {
          margin-right: 0;
        }

        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;

          &:hover {
            background: #e3f9f4;
          }

          img {
            width: 95px;
            height: 95px;
          }

          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;

            .name {
              font-size: 16px;
              color: #666;
            }

            .desc {
              color: #999;
            }

            .price {
              font-size: 22px;
              color: @priceColor;

              i {
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }

  // 鼠标悬浮时让商品推荐的弹层显示
  &:hover {
    .layer {
      display: block;
    }
  }

  // 菜单样式
  .menu {
    li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;

      &:hover {
        background: @xtxColor;
      }

      // 确保鼠标移入分类推荐商品时不至于丢失
      &.active {
        background: @xtxColor;
      }

      a {
        margin-right: 4px;
        color: #fff;

        // 一级分类的文字
        &:first-child {
          font-size: 16px;
        }
      }
    }
  }
}
</style>